package com.fileupdown;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/fileupload3")
    public String fileupload3(MultipartFile upload) throws IOException {
        System.out.println(">>>>>>>>跨服务器上传<<<<<<<<<<<");
        //1定义需要上传的服务器路径
        String path="http://localhost:8081/";
        //2获取上传文件的名称
        String Filename = upload.getOriginalFilename();
        //3完成上传
        //3.1需要用到jersey-core的jar包
        //3.2创建客户端的对象
        Client client=Client.create();
        WebResource resource = client.resource(path + Filename);//链接服务器
        System.out.println(resource.toString());
        //上传文件
        resource.put(upload.getBytes());
        return "success";
    }
    /**
     * upload 必须和前端的name值一样
     * springmvc的方式
     */
    @RequestMapping("fileupload2")
    public String fileupload2(HttpServletRequest request, MultipartFile upload) throws Exception{
        System.out.println(">>>>>>>>>>>mvc上传<<<<<<<<<");
        //1去spring 的配置文件 配置文件解析器 CommonsMultipartResolver
        //2获取上传的文件路径位置
      //springmvc的方式就不能再使用request的方式了 需要去spring的配置文件中配置  request.setCharacterEncoding("utf-8");
        String realPath = request.getSession().getServletContext().getRealPath("/up/");
        File file=new File(realPath);
        if (!file.exists()){
            file.mkdirs();
        }
        //获取上传文件的名称
        String Filename = upload.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("_", "");
        Date date=new Date();
        String dates =date.getHours()+"_"+date.getMinutes()+"_"+date.getSeconds();
        Filename=dates.toString()+"_"+uuid.substring(0,2)+"_"+Filename;//截取uuid的前两位
        upload.transferTo(new File(realPath,Filename));
        System.out.println("上传的位置"+realPath+Filename);
        return "success";
    }
    /**
     * 传统的httpservletRequest的方式传文件
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/fileupload1")
    public String fileupload1(HttpServletRequest request) throws Exception {
        System.out.println(">>>>>>>>>>>文件上传<<<<<<<<<");
        //1使用fileload组件完成文件上传
        request.setCharacterEncoding("utf-8");//通过request来进行中文的设置
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");//上传的位置
        File file=new File(realPath);
        if(!file.exists()){
            //如果没有创建一个文件
            file.mkdirs();
        }
        //2解析request对象，获取上传文件向
        DiskFileItemFactory fileItemFactory=new DiskFileItemFactory();//工厂对象。磁盘文件项
        ServletFileUpload upload=new ServletFileUpload(fileItemFactory);
        //解析request
        List<FileItem> fileItems = upload.parseRequest(request);
        for (FileItem fileItem:fileItems){
            //进行判断，当前item对象是否是上传文件项
            if(fileItem.isFormField()){
                    //为true说明是表单项
            }else {
                //文件项
                String name = fileItem.getName();//获取文件名称
                //使用uuid弄文件名的唯一值
                String uuid = UUID.randomUUID().toString().replace("_", "");//replace是替换字符串
                name=uuid.substring(0,2)+"_"+name;//截取uuid的前两位
                //完成上传(传进路径和文件）
                fileItem.write(new File(realPath,name));
                System.out.println("上传的文件路径："+realPath+name);
                //删除临时文件  大于10k就有临时文件，需要删除
                fileItem.delete();
            }
        }
        return "success";
    }
}
